package com.genntii.examManager.controller;

import com.genntii.examManager.common.constant.CommonConstant;
import com.genntii.examManager.common.result.PageResult;
import com.genntii.examManager.common.result.Result;
import com.genntii.examManager.domain.question.dto.ExamResult;
import com.genntii.examManager.domain.question.vo.ExamDetailVO;
import com.genntii.examManager.domain.vo.PracticeRecordVO;
import com.genntii.examManager.domain.vo.PracticeVO;
import com.genntii.examManager.service.PracticeRecordService;
import com.genntii.examManager.service.PracticeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/practice")
public class PracticeController {

    @Resource
    private PracticeService practiceService;

    @Resource
    private PracticeRecordService practiceRecordService;

    @GetMapping("/list")
    public Result<PageResult<PracticeVO>> getExamList(@RequestParam("pageNum") Integer pageNum,
                                                      @RequestParam("pageSize") Integer pageSize,
                                                      @RequestParam("courseId") Long courseId,
                                                      HttpServletRequest request){
        Long userId = request.getAttribute(CommonConstant.USER_ID) != null ? Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.USER_ID))) : Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.TEACHER_ID)));
        return Result.success(practiceService.getPracticeList(pageNum,pageSize,userId,courseId));
    }

    @GetMapping("/{practiceId}")
    public Result<ExamDetailVO> practice(@PathVariable Long practiceId){
        return Result.success(practiceService.getPractice(practiceId));
    }

    @GetMapping("/history")
    public Result<PageResult<PracticeRecordVO>> getHistoryList(@RequestParam("pageNum") Integer pageNum,
                                                              @RequestParam("pageSize") Integer pageSize,
                                                              HttpServletRequest request){
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.USER_ID))) ;
        return Result.success(practiceRecordService.getPracticeHistory(pageNum,pageSize,userId));
    }

    @PostMapping
    public Result commit(@RequestBody ExamResult practiceResult, HttpServletRequest request){
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.USER_ID))) ;
        practiceRecordService.practiceCommit(practiceResult,userId);
        return Result.success();
    }

    @GetMapping("/history/{practiceRecordId}")
    public Result<ExamResult> historyHistory(@PathVariable Long practiceRecordId){
        return Result.success(practiceRecordService.getPracticeResult(practiceRecordId));
    }
}
